<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/11 0011
 * Time: 下午 20:02
 */
namespace Home\Controller;
use Core\Controller;
use Core\DB;
class ApiController extends Controller{
    protected $userid = '';
    public function __construct(){
        $rs = $this->getuserid($_POST['username']);
        if(empty($rs)){
            $result = create_label("用户不存在!");
            echo(json_encode($result));exit;
        }
        $this->userid = $rs['id'];
    }
    public function index(){
        $type = trim($_POST['type']);
        $this->$type();
    }

    public function getuserid($username=''){
        $where['username'] = $username;
        return DB::table('user')->where($where)->find();
    }

    public function paylist(){
        $where['userid'] = $this->userid;
        $where['status#in'] = [1,2];
        if($_POST['starttime']){
            $where['create_time#>='] = trim($_POST['starttime']);
        }
        if($_POST['endtime']){
            $where['create_time#<='] = trim($_POST['endtime']);
        }
        $data =  DB::table("finanpay")->field("id,userid,num,tradeno,create_time,status")->where($where)->select();
        $sum =  DB::table("finanpay")->where($where)->sum("num");
        $title = "用户<nobr style='color: red;'>{$_POST['username']}</nobr>充值金额总和:<nobr style='color: red;'>￥".$sum."</nobr>";
        $th = array(
            'id'=>"ID",
            'userid'=>"用户ID",
            'num'=>"充值金额",
            'tradeno'=>"订单号",
            'create_time'=>"充值时间",
            'status'=>"到帐类型");
        $result = create_table($title,$th,$data);
        echo(json_encode($result));exit;
    }

    public function drawalslist(){
        $where['user_id'] = $this->userid;
        $where['status#in'] = [0,2];
        if($_POST['starttime']){
            $where['create_time#>='] = trim($_POST['starttime']);
        }
        if($_POST['endtime']){
            $where['create_time#<='] = trim($_POST['endtime']);
        }
        $data =  DB::table("withdrawals")->field("id,user_id,num,create_time,status")->where($where)->select();
        unset($where['status#in']);
        $where['status'] = 2;
        $withed =  DB::table("withdrawals")->where($where)->sum("num");
        $where['status'] = 0;
        $withing =  DB::table("withdrawals")->where($where)->sum("num");
        $title = "用户<nobr style='color: red;'>{$_POST['username']}</nobr>已提现金额总和:<nobr style='color: red;'>￥".$withed."</nobr>";
        if($withing){
            $title .= "提现申请中金额总和:￥<nobr style='color: red;'>".$withed."</nobr>";
        }
        $th = array(
            'id'=>"ID",
            'user_id'=>"用户ID",
            'num'=>"提现金额",
            'create_time'=>"提现时间",
            'status'=>"提现状态");
        $result = create_table($title,$th,$data);
        echo(json_encode($result));exit;
    }
    public function buylist(){
        $where['entrust_buyuserid'] = $this->userid;
        $where['market_id'] = 1;
        if($_POST['starttime']){
            $where['create_time#>='] = trim($_POST['starttime']);
        }
        if($_POST['endtime']){
            $where['create_time#<='] = trim($_POST['endtime']);
        }
        $data =  DB::table("turnoverorder")->field("id,entrust_buyuserid,turnover_num,turnover_price,create_time,turnover_amount")->where($where)->select();
        $amount =  DB::table("turnoverorder")->where($where)->sum("turnover_amount");
        $fee =  DB::table("turnoverorder")->where($where)->sum("turnover_buyfee");
        $num =  DB::table("turnoverorder")->where($where)->sum("turnover_num");
        $sum = $amount+$fee;
        $title = "用户<nobr style='color: red;'>{$_POST['username']}</nobr>共买入喜宝积分<nobr style='color: red;'>{$num}</nobr>个,花去金额:<nobr style='color: red;'>￥".$sum."</nobr>";
        $th = array(
            'id'=>"ID",
            'entrust_buyuserid'=>"用户ID",
            'turnover_num'=>"买入数量",
            'turnover_price'=>"交易价格",
            'turnover_amount'=>"交易总额",
            'create_time'=>"交易时间");
        $result = create_table($title,$th,$data);
        echo(json_encode($result));exit;
    }
    public function selllist(){
        $where['entrust_selluserid'] = $this->userid;
        $where['market_id'] = 1;
        if($_POST['starttime']){
            $where['create_time#>='] = trim($_POST['starttime']);
        }
        if($_POST['endtime']){
            $where['create_time#<='] = trim($_POST['endtime']);
        }
        $data =  DB::table("turnoverorder")->field("id,entrust_selluserid,turnover_num,turnover_price,create_time,turnover_amount")->where($where)->select();
        $amount =  DB::table("turnoverorder")->where($where)->sum("turnover_amount");
        $fee =  DB::table("turnoverorder")->where($where)->sum("turnover_sellfee");
        $num =  DB::table("turnoverorder")->where($where)->sum("turnover_num");
        $sum = $amount-$fee;
        $title = "用户<nobr style='color: red;'>{$_POST['username']}</nobr>共卖出喜宝积分<nobr style='color: red;'>{$num}</nobr>个,获得总金额:<nobr style='color: red;'>￥".$sum."</nobr>";
        $th = array(
            'id'=>"ID",
            'entrust_selluserid'=>"用户ID",
            'turnover_num'=>"买入数量",
            'turnover_price'=>"交易价格",
            'turnover_amount'=>"交易总额",
            'create_time'=>"交易时间");
        $result = create_table($title,$th,$data);
        echo(json_encode($result));exit;
    }
    public function inwallet(){
        $where['userid'] = $this->userid;
        $where['status'] = 1;
        if($_POST['starttime']){
            $where['create_time#>='] = trim($_POST['starttime']);
        }
        if($_POST['endtime']){
            $where['create_time#<='] = trim($_POST['endtime']);
        }
        $data =  DB::table("inwallet")->field("id,userid,num,create_time,txid")->where($where)->limit(10)->select();
        $sum =  DB::table("inwallet")->where($where)->sum("num");
        $sum = number_format($sum,2,'.','');
        $title = "用户<nobr style='color: red;'>{$_POST['username']}</nobr>共转入喜宝币总和:<nobr style='color: red;'>".$sum."</nobr>个";
        $th = array(
            'id'=>"ID",
            'userid'=>"用户ID",
            'num'=>"转入数量",
            'create_time'=>"转入时间",
            'txid'=>"txid值");
        $result = create_table($title,$th,$data);
        echo(json_encode($result));exit;
    }
    public function outwallet(){
        $where['userid'] = $this->userid;
        $where['status'] = 1;
        if($_POST['starttime']){
            $where['create_time#>='] = trim($_POST['starttime']);
        }
        if($_POST['endtime']){
            $where['create_time#<='] = trim($_POST['endtime']);
        }
        $data =  DB::table("outwallet")->field("id,userid,num,create_time,txid")->where($where)->limit(10)->select();
        $sum =  DB::table("outwallet")->where($where)->sum("num");
        $sum = number_format($sum,2,'.','');
        $title = "用户<nobr style='color: red;'>{$_POST['username']}</nobr>共转出喜宝币总和:<nobr style='color: red;'>".$sum."</nobr>个";
        $th = array(
            'id'=>"ID",
            'userid'=>"用户ID",
            'num'=>"转入数量",
            'create_time'=>"转入时间",
            'txid'=>"txid值");
        $result = create_table($title,$th,$data);
        echo(json_encode($result));exit;
    }
}
